package cate.game.role.res.crystalldraw;

import cate.common.table.d.GDCrystallDraw;
import cate.common.table.res.crystalldraw.CrystallDrawRewardRow;
import cate.common.util.GameResult;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.res.crystalldraw.msg.CrystallDrawDataResp;
import easy.java.dev.note.NoteField;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyCrystallDraw extends RoleDependent {
    @NoteField("今日钻石抽奖次数")
    public int m1CallTimes;

    @NoteField("使用的免费次数")
    public int freeTimes;

    @NoteField("抽奖次数")
    public int callTimes;

    @NoteField("奖励领取记录 ")
    public Map<Integer, Boolean> gotMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (gotMap == null) {
            gotMap = new HashMap<>();
        }
    }

    public GameResult<Void> draw(int type) {
        GameResult<Void> r = new GameResult<>();
        if (type != GDCrystallDraw.ONE_TYPE && type != GDCrystallDraw.TEN_TYPE) {
            return r.fail("错误的抽奖类型");
        }
        int times = type == GDCrystallDraw.ONE_TYPE ? 1 : 10;
        if (type == GDCrystallDraw.ONE_TYPE && freeTimes < GDCrystallDraw.FREE_TIMES) {
            freeTimes ++;
        } else {
            GameResult<MixResItem> consumeRes = new MixRes(GDCrystallDraw.ONE_TIME_CONSUME_STR).multiply(times).consume(role);
            if (!consumeRes.ok()) {
                if (m1CallTimes + times <= GDCrystallDraw.M1_TIMES) {
                    consumeRes = new MixRes(GDCrystallDraw.ONE_TIME_M1_CONSUME_STR).multiply(times).consume(role);
                    if (consumeRes.ok()) {
                        m1CallTimes += times;
                        new MixResAdder().setRes(new MixRes(GDCrystallDraw.REWARD_STR).multiply(times)).exe(role);
                    }
                } else {
                    return r.fail("钻石寻宝次数不足");
                }
                if (!consumeRes.ok()) {
                    role.getGame().notice.tipResItem(role, consumeRes.data);
                    return r.fail(consumeRes.message);
                }
            }
        }
        callTimes += times;
        List<String> rewardRows = role.getGame().table.crystallDraw.weight.randomRewards(times);
        MixRes reward = new MixRes();
        for (String rewardStr : rewardRows) {
            reward.addList(new MixRes(rewardStr));
        }
        new MixResAdder().setRes(reward).setDialogReward(true).setSendMailWhenFull(true).exe(role);
        noticeUpdate();
        role.getHistory().action.crystallDrawTimes(times);
        return r;
    }

    public GameResult<Void> getReward(int tid) {
        GameResult<Void> r = new GameResult<>();
        if (gotMap.containsKey(tid)) {
            return r.fail("您已经领取过该奖励");
        }
        CrystallDrawRewardRow rewardRow = role.getGame().table.crystallDraw.reward.get(tid);
        if (rewardRow == null) {
            return r.fail("奖励不存在");
        }
        if (callTimes < rewardRow.times) {
            return r.fail("未达到奖励领取条件");
        }
        new MixResAdder()
                .setRes(rewardRow.rewardStr)
                .setSendMailWhenFull(true)
                .setDialogReward(true)
                .exe(role);
        gotMap.put(tid, true);
        if (gotMap.size() == role.getGame().table.crystallDraw.reward.getList().size()) {
            gotMap.clear();
            CrystallDrawRewardRow maxRow = role.getGame().table.crystallDraw.reward.getList().stream().max(Comparator.comparingInt(e -> e.times)).orElse(null);
            callTimes -= maxRow == null ? 0 : maxRow.times;
        }
        noticeUpdate();
        return r;
    }

    public void noticeUpdate() {
        role.sendNow(new CrystallDrawDataResp(this));
    }

    @Override
    public void onDaySpan(boolean silence) {
        m1CallTimes = 0;
        freeTimes = 0;
        noticeUpdate();
    }
}
